using UnityEngine;
using QFramework;
using System;
using QAssetBundle;
using Unity.Burst.Intrinsics;

namespace ProjectSurvivor
{
	public partial class Enemy : ViewController,IEnemy//表示一个挂在场景物体上的控制器
	{
		public float speed = 1;
		public float HP = 3f;
		public Color DissolveColor;
		public bool mTgnoreHurt;
		public bool TreasureChesEnemy1=false;
		void Start()
		{
			EnemyGenerator.EnemyCount.Value++;
		}
        void OnDestroy()
        {
            EnemyGenerator.EnemyCount.Value--;
        }
		void FixedUpdate()//用 FixedUpdate 能让运动更稳定、符合物理规则
		{
			if (!mTgnoreHurt)
			{
				if (Player.Default)
				{
					var direction = (Player.Default.transform.position - transform.position).normalized;
					SelfRigidbody2D.velocity = direction * speed;
				}
				else
				{
					SelfRigidbody2D.velocity = Vector3.zero * speed;
				}
			}
			
        }
        void Update()
		{
			
			if (HP <= 0)
			{
				if (gameObject != null&&true)
				{
					Global.GeneratePowerUp(gameObject,TreasureChesEnemy1);
					FxController.Play(Triangle,DissolveColor);
					AudioKit.PlaySound(Sfx.ENEMY_DIE);
					this.DestroyGameObjGracefully();
				}
				
			}
        }

		
        public void Hurt(float value,bool force=false,bool citical=false)
		{
			if (mTgnoreHurt&&!force) return;
			FloattingTextController.Play(transform.position+Vector3.up*0.5f,value.ToString("0"),citical);
			Triangle.color = Color.red;
			mTgnoreHurt = true;
			SelfRigidbody2D.velocity = Vector2.zero;
			AudioKit.PlaySound("hit");
			ActionKit.Delay(0.2f, () =>//注册帧更新/UI调试回调；
			{
				HP -= value;
				Triangle.color = Color.white;
				mTgnoreHurt = false;

			}).Start(this);

		}

        public void SetHpScale(float hpScale)
        {
			HP *= hpScale;
        }

        public void SetSpeesScale(float speedScale)
        {
            speed *= speedScale;
        }

        public void SetHPScale(float speedScale)
        {
            throw new NotImplementedException();
        }
    }
}
